const int MAXN=1010,MAXM=1000010;
int n;
struct graph
{
	int a[MAXN],b[MAXM],c[MAXM],d[MAXM],p;
	int dfn[MAXN],low[MAXN],rec[MAXN],sta[MAXN],instack[MAXN],lsta,ind,col;
	int tarjan(int k)
	{
		dfn[k]=low[k]=++ind;
		instack[k]=1;
		sta[++lsta]=k;
		for(int i=a[k];i;i=c[i])
		{
			if (!dfn[b[i]]) low[k]=min(low[k],tarjan(b[i]));
			else if (instack[b[i]]) low[k]=min(low[k],low[b[i]]);
		}
		if (dfn[k]==low[k])
		{
			col++;
			while(sta[lsta]!=k)
			{
				rec[sta[lsta]]=col;
				instack[sta[lsta]]=0;
				lsta--;
			}
			rec[k]=col;
			instack[k]=0;
			lsta--;
		}
		return low[k];
	}
	void init()
	{
		memset(dfn,0,sizeof dfn);
		memset(rec,0,sizeof rec);
		ind=0; col=0;
		for(int i=1;i<=n;i++) if (!dfn[i])
			tarjan(i);
	}
};
